package com.jinlejia.erp.ui.mine.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.freedom.yefeng.yfrecyclerview.YfListAdapter;
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder;
import com.jinlejia.erp.R;
import com.jinlejia.erp.databinding.ItemMyContractBinding;
import com.squareup.picasso.Picasso;
import com.wang.avi.AVLoadingIndicatorView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

import androidx.databinding.DataBindingUtil;

import static com.jinlejia.erp.base.ApiConfig.MAGEHEADER_URL;


/**
 * 我的合同（adapter）
 */

public class MyContractAdapter extends YfListAdapter<Map<String, Object>> {

    private Context context;

    public MyContractAdapter(ArrayList<Map<String, Object>> data) {
        super(data);
    }

    @Override
    public RecyclerView.ViewHolder onCreateDataViewHolder(ViewGroup parent) {
        context = parent.getContext().getApplicationContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_my_contract, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateEmptyViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_layout, parent, false);
        TextView txt = (TextView) view.findViewById(R.id.id_tips);
        txt.setText("暂无数据");
        return new YfSimpleViewHolder(view);
    }

    @Override
    public void onBindDataViewHolder(final RecyclerView.ViewHolder viewHolder, int position) {
        ItemMyContractBinding binding =  DataBindingUtil.getBinding(viewHolder.itemView);
        Map<String, Object> map = mData.get(position);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        if (map.containsKey("imageUrl") && !TextUtils.isEmpty(map.get("imageUrl").toString()))  //房源图片
            Picasso.with(context).load(MAGEHEADER_URL().concat(map.get("imageUrl").toString())).placeholder(R.mipmap.ic_no_img).into(binding.shootIcon);
        if (map.containsKey("contractCode"))  //合同编号
            binding.contractNumber.setText(map.get("contractCode").toString());
        if (map.containsKey("tradeTime"))     //成交日期
            binding.time.setText(sdf.format(new Date(Long.valueOf(map.get("tradeTime").toString()))));
        if (map.containsKey("address"))       //物业地址
            binding.address.setText(map.get("address").toString());
        if (map.containsKey("customerName"))  //客户姓名
            binding.customerName.setText(map.get("customerName").toString());
        if (map.containsKey("owner"))         //业主名称
            binding.ownerName.setText(map.get("owner").toString());

        if (map.containsKey("tradePrice") && map.get("contractType").toString().equals("1"))    //成交价
            binding.ownerName.setText(map.get("tradePrice").toString());

        if (map.containsKey("contractType")) {
            switch (map.get("contractType").toString()) {
                case "0":           //买卖
                    if (map.containsKey("tradePrice"))         //成交价
                        binding.money.setText(map.get("tradePrice").toString().concat("万"));
                    break;
                case "1":           //租赁
                    if (map.containsKey("rentPrice"))         //租赁价
                        binding.money.setText(map.get("rentPrice").toString().concat("元"));
                case "2":
                    if (map.containsKey("tradePrice"))         //成交价
                        binding.money.setText(map.get("tradePrice").toString().concat("万"));
                    break;
                case "3":
                    if (map.containsKey("tradePrice"))         //成交价
                        binding.money.setText(map.get("tradePrice").toString().concat("万"));
                    break;
                    default:
                        break;
            }
        }
        if (map.containsKey("executeState")) {  //审核状态
            binding.state.setTextColor(context.getResources().getColor(R.color.red));
            switch (map.get("executeState").toString()) {
                case "0":
                    binding.state.setText("草稿");
                    break;
                case "1":
                    binding.state.setText("经管中心待审核");
                    break;
                case "2":
                    binding.state.setText("审核通过");
                    binding.state.setTextColor(context.getResources().getColor(R.color.blue));
                    break;
                case "3":
                    binding.state.setText("经管中心驳回");
                    break;
                case "4":
                    binding.state.setText("交易中心待审核");
                    break;
                case "5":
                    binding.state.setText("交易中心驳回");
                    break;
                case "6":
                    binding.state.setText("已退单");
                    break;
                    default:
                        break;
            }
        }
        binding.getRoot().setTag(mData.get(position));
    }

    @Override
    public RecyclerView.ViewHolder onCreateLoadingViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.loading_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateFooterViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.footer_loading, parent, false);
        return new FooterViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateErrorViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.error_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public void onBindFooterViewHolder(RecyclerView.ViewHolder holder, int position) {
        super.onBindFooterViewHolder(holder, position);
        if (mFooters.get(0).equals(R.string.is_all)) {
            ((FooterViewHolder) holder).txt.setText(R.string.is_all);
            ((FooterViewHolder) holder).txt.setVisibility(View.VISIBLE);
            ((FooterViewHolder) holder).progress.setVisibility(View.GONE);
        } else {
            ((FooterViewHolder) holder).txt.setVisibility(View.GONE);
            ((FooterViewHolder) holder).progress.setVisibility(View.VISIBLE);
        }
    }

    static final class FooterViewHolder extends RecyclerView.ViewHolder {
        TextView txt;
        AVLoadingIndicatorView progress;

        public FooterViewHolder(View itemView) {
            super(itemView);
            txt = (TextView) itemView.findViewById(R.id.txt);
            progress = (AVLoadingIndicatorView) itemView.findViewById(R.id.progress);
        }
    }

    static final class ViewHolder extends RecyclerView.ViewHolder {

        public ViewHolder(View itemView) {
            super(itemView);
            DataBindingUtil.bind(itemView);
        }
    }

}
